#ifndef _protocol_grapmotor_h_
#define _protocol_grapmotor_h_

#include "protocol.h"
#include "CRC_Generate.h"


#define		Larger_grap			(0)
#define		Smaller_grap		(1)
/*
note: this part is reference from modbus
sci communication parameter:
speed:9600
checksum:even
stopbit:1
slave add:0x01
*/
typedef struct grapmotor_result{
	uint16_t	pwm;
	uint16_t	current;
	int16_t		freq;
	uint16_t	pos_cont_status;
	uint16_t	pos_high_half;
	uint16_t	pos_low_half;
	uint16_t	res_time_high_half;
	uint16_t	res_time_low_half;
	uint16_t	in1_volt;
	uint16_t	in2_volt;
	uint16_t	in3_volt;
	uint16_t	div_volt;
	uint16_t	sq1_volt;
	uint16_t	sq2_volt;
	uint16_t	in1_input_pwmduty;
	uint16_t	in1_input_freq;
	uint16_t	in1_input_pulsecount_high_half;
	uint16_t	in1_input_pulsecount_low_half;
	uint16_t	halt_status;
	uint16_t	error_status;
	uint16_t	motor_speed;
	uint16_t	motor_speed_ratio;
}grap_resultInfo, *grap_PresultInfo;
typedef struct grapmotor_info{
	uint8_t  timer;
	uint8_t  count;
	uint8_t  len;
	uint8_t  step;
	uint16_t crc16;
	uint16_t crc16_T;
}grap_info,*grap_Pinfo;
extern grap_resultInfo	grapmotor_result;


#define 	grapmotor_ID		(2)
#define   	grapmotor_group		(3)


extern uint8_t grapmotor_process(pmesg_prot mesg);

extern uint8_t grapmotor_rxFunc(void);

extern uint8_t grapmotor_txFunc(void);

extern uint8_t grapmotor_period_callback(void);
/*
ask:
01 03 00 20 00 15 85 CF 
resp:
01 03 2A 
00 00 00 00 00 
00 00 01 FF FF 
F8 0D 00 00 00 
00 0C DE 0C DE 
0C E1 00 00 00 
01 00 01 03 E8 
00 00 00 00 00 
00 00 00 00 00 
FF FF 
E7 1C
note: 
    the u8_grapmotor_idealtimer should be update by timer
*/
extern void grapmotor_rxFunc_callback(uint8_t data_rx);
/*
Cmds
*/

/*
status check:01 03 00 20 00 15 85 CF
*/
extern uint8_t grapmotor_status_Get(void);
/*
setting speed and position:
    01 10 00 44 00 04 08 03 E8 00 01 00 00 00 F4 52 D6 
note:
  the speedHz is 0.1hz为精度
  the positionLen has signed  
*/
extern uint8_t grapmotor_speedandposition_Set(uint16_t speedHz, uint16_t positionMode, uint32_t positionLen);
/*
ErrorStopTime->0.1s
NormalCur->0.01A
MaxCur->0.01A
StopCur->0.01A
LostStopTime->0.1s
*/
extern uint8_t grapmotor_Sysconfigure(uint8_t ErrorStopTime, uint16_t NormalCur, uint16_t MaxCur, uint16_t StopCur, uint8_t LostStopTime);

extern void grapmotor_init(FunType_bufferTx txFunc);
extern uint8_t grapmotor_firstPosition_Set(void);
extern uint8_t grapmotor_step_setting(uint8_t dirc, uint16_t sped, uint16_t time);
extern uint8_t grapmotor_sped_setting(uint8_t dirc, uint16_t sped);
extern uint8_t Ingrap_Action_grap_v2(void);
extern uint8_t Ingrap_Action_grap(void);

extern uint8_t grapmotor_interfaceTest(void);
extern uint8_t grapmotor_task(void);
extern void grapmotor_lowTxfunc_Register(FunType_bufferTx txFunc);

#endif
